//
//  SABaseInfoVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/8/18.
//

#import "SABaseInfoVC.h"
#import "YYTextView.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SASelectAddressView.h"
#import "SAPayRegisterDetailModel.h"
#import "SAPayRegisterAreaModel.h"
#import <TZImagePickerController.h>
#import <AipOcrSdk/AipOcrSdk.h>
#import "SAPayCommonUploadAPI.h"
#import "SAPayRegisterBaseInfoAPI.h"
#import "SAPayRegisterDetailAPI.h"
#import "SAAccountInfoVC.h"
#import "QRCodePermissions.h"
#import "NSString+FintCommon.h"
#import "SAConsultationWechatVC.h"

@interface SABaseInfoVC ()<YYTextViewDelegate, HTTPAPIDelegate>

@property (nonatomic, strong) YYTextView *merchantNameTv;//商户名称
@property (nonatomic, strong) YYTextView *merchantAsNameTv;//商户简称
@property (nonatomic, strong) YYTextView *cityTv;
@property (nonatomic, strong) YYTextView *adressTv;
@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic, strong) SASelectAddressView *selectAddressView;

@property (nonatomic, strong) SAPayRegisterDetailModel *model;
@property (nonatomic, strong) NSMutableArray<SAPayRegisterAreaModel *> *areaMarr;

@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIImageView *imageV2;
@property (nonatomic, strong) UIImageView *imageV3;
//图片地址
@property (nonatomic, strong) NSString *doorPic;
@property (nonatomic, strong) NSString *insidePic;
@property (nonatomic, strong) NSString *cashierDeskPic;

@property (nonatomic, strong) SAPayCommonUploadAPI *payCommonUploadAPI;
@property (nonatomic, strong) SAPayCommonUploadAPI *payCommonUploadAPI2;
@property (nonatomic, strong) SAPayCommonUploadAPI *payCommonUploadAPI3;

@property (nonatomic, strong) NSMutableArray *regionMarr;

@property (nonatomic, strong) SAPayRegisterBaseInfoAPI *payRegisterBaseInfoAPI;
@property (nonatomic, strong) SAPayRegisterDetailAPI *payRegisterDetailAPI;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UIButton *merchantNameBtn;

@property (nonatomic, strong) UIButton *merchantCloseBtn;
@property (nonatomic, strong) UIButton *nameCloseBtn;
@property (nonatomic, strong) UIButton *adressCloseBtn;

@end

@implementation SABaseInfoVC

-(instancetype)initWithModel:(SAPayRegisterDetailModel *)model {
    if (self = [super init]) {
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.payCommonUploadAPI == api) {
        [HDHubView dismiss];
        NSString *imgURL = data[@"data"][@"imgURL"];
        [self.imageV sd_setImageWithURL:[NSURL URLWithString:imgURL]];
        self.doorPic = data[@"data"][@"fileId"];
        [self showNext];
    }else if (self.payCommonUploadAPI2 == api) {
        [HDHubView dismiss];
        NSString *imgURL = data[@"data"][@"imgURL"];
        [self.imageV2 sd_setImageWithURL:[NSURL URLWithString:imgURL]];
        self.insidePic = data[@"data"][@"fileId"];
        [self showNext];
    }else if (self.payCommonUploadAPI3 == api) {
        [HDHubView dismiss];
        NSString *imgURL = data[@"data"][@"imgURL"];
        [self.imageV3 sd_setImageWithURL:[NSURL URLWithString:imgURL]];
        self.cashierDeskPic = data[@"data"][@"fileId"];
        [self showNext];
    }else if (self.payRegisterBaseInfoAPI == api) {
        [self.payRegisterDetailAPI sendRequestToServerWithActivityViewAndMask:POST];
    }else if (self.payRegisterDetailAPI == api) {
        self.model = [SAPayRegisterDetailModel mj_objectWithKeyValues:data[@"data"]];
        SAAccountInfoVC *pushVC = [[SAAccountInfoVC alloc] initWithModel:self.model];
        [self.navigationController pushViewController:pushVC animated:YES];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    if (self.payCommonUploadAPI == api) {
        [HDHubView dismiss];
        [Prompt popPromptViewWithMsg:@"图片上传失败" duration:2];
    }else if (self.payCommonUploadAPI2 == api) {
        [HDHubView dismiss];
        [Prompt popPromptViewWithMsg:@"图片上传失败" duration:2];
    }else if (self.payCommonUploadAPI3 == api) {
        [HDHubView dismiss];
        [Prompt popPromptViewWithMsg:@"图片上传失败" duration:2];
    }
}

#pragma mark - UITextFieldDelegate
- (void)textViewDidChange:(YYTextView *)textView {
    NSString *newStr = [textView.text noEmoji];
    if (textView.text.length != newStr.length) {
        textView.text = newStr;
    }
    [self contentSizeToFit:textView];
    [self showNext];
    
    if (self.merchantNameTv == textView) {
        self.merchantCloseBtn.hidden = (textView.text.length == 0);
    }else if (self.merchantAsNameTv == textView) {
        self.nameCloseBtn.hidden = (textView.text.length == 0);
    }else if (self.adressTv == textView) {
        self.adressCloseBtn.hidden = (textView.text.length == 0);
    }
}

- (BOOL)textViewShouldBeginEditing:(YYTextView *)textView {
    if (self.merchantNameTv == textView) {
        self.merchantCloseBtn.hidden = (textView.text.length == 0);
    }else if (self.merchantAsNameTv == textView) {
        self.nameCloseBtn.hidden = (textView.text.length == 0);
    }else if (self.adressTv == textView) {
        self.adressCloseBtn.hidden = (textView.text.length == 0);
    }
    return YES;
}

- (void)textViewDidEndEditing:(YYTextView *)textView {
    if (self.merchantNameTv == textView) {
        self.merchantCloseBtn.hidden = YES;
    }else if (self.merchantAsNameTv == textView) {
        self.nameCloseBtn.hidden = YES;
    }else if (self.adressTv == textView) {
        self.adressCloseBtn.hidden = YES;
    }
}

#pragma mark - event
-(void)doorImage {
    
    if (![QRCodePermissions allowCamera]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请开启摄像头权限" message:@"请在\"设置 - 隐私 - 相机\"选项中，允许访问您的相机" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        [alert addAction:action];
        [alert addAction:action2];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [[AipOcrService shardService] clearCache];
    UIViewController * vc = [AipGeneralVC ViewControllerWithHandler:^(UIImage *image) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if (image) {
                [HDHubView loadingMessage:@"正在上传图片,请稍等..."];
                [weakSelf.payCommonUploadAPI sendServerImageData:image];
            }
            [weakSelf showNext];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
    }];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

-(void)insideImage {
    
    if (![QRCodePermissions allowCamera]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请开启摄像头权限" message:@"请在\"设置 - 隐私 - 相机\"选项中，允许访问您的相机" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        [alert addAction:action];
        [alert addAction:action2];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [[AipOcrService shardService] clearCache];
    UIViewController * vc = [AipGeneralVC ViewControllerWithHandler:^(UIImage *image) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if (image) {
                [HDHubView loadingMessage:@"正在上传图片,请稍等..."];
                [weakSelf.payCommonUploadAPI2 sendServerImageData:image];
            }
            [weakSelf showNext];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
    }];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

-(void)cashierDeskImage {
    
    if (![QRCodePermissions allowCamera]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请开启摄像头权限" message:@"请在\"设置 - 隐私 - 相机\"选项中，允许访问您的相机" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }];
        [alert addAction:action];
        [alert addAction:action2];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [[AipOcrService shardService] clearCache];
    UIViewController * vc = [AipGeneralVC ViewControllerWithHandler:^(UIImage *image) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if (image) {
                [HDHubView loadingMessage:@"正在上传图片,请稍等..."];
                [weakSelf.payCommonUploadAPI3 sendServerImageData:image];
            }
            [weakSelf showNext];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
    }];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

-(void)clickRightBtn {
    [self.view endEditing:YES];
    [self.selectAddressView refreshData:self.areaMarr AndRegionMarr:self.regionMarr];
    self.selectAddressView.hidden = !self.selectAddressView.isHidden;
}

-(void)showNext {
    BOOL isShow = YES;
    if (self.doorPic.length == 0) {
        isShow = NO;
    }
    if (self.insidePic.length == 0) {
        isShow = NO;
    }
    if (self.cashierDeskPic.length == 0) {
        isShow = NO;
    }
    if (self.merchantNameTv.text.length == 0) {
        isShow = NO;
    }
    if (self.merchantAsNameTv.text.length == 0) {
        isShow = NO;
    }
    if (self.cityTv.text.length == 0) {
        isShow = NO;
    }
    if (self.adressTv.text.length == 0) {
        isShow = NO;
    }
    
    if (isShow == YES) {
        self.nextBtn.userInteractionEnabled = YES;
        [self.nextBtn setBackgroundImage:[UIImage imageNamed:@"button_blue_bg"] forState:UIControlStateNormal];
    }else {
        self.nextBtn.userInteractionEnabled = NO;
        [self.nextBtn setBackgroundImage:[UIImage imageNamed:@"button_gray_bg"] forState:UIControlStateNormal];
    }
}

-(void)clickedNextBtn {
    
    if ([self.model.merchantDetailVO.auditStatus isEqualToString:@"0"] || [self.model.merchantDetailVO.auditStatus isEqualToString:@"1"]) {
        SAAccountInfoVC *pushVC = [[SAAccountInfoVC alloc] initWithModel:self.model];
        [self.navigationController pushViewController:pushVC animated:YES];
        return;
    }
    
    SAPayRegisterAreaModel *model1 = self.areaMarr[0];
    SAPayRegisterAreaModel *model2 = self.areaMarr[1];
    SAPayRegisterAreaModel *model3 = self.areaMarr[2];
    
    self.payRegisterBaseInfoAPI.province = model1.areaName;
    self.payRegisterBaseInfoAPI.provinceCode = model1.areaCode;
    self.payRegisterBaseInfoAPI.city = model2.areaName;
    self.payRegisterBaseInfoAPI.cityCode = model2.areaCode;
    self.payRegisterBaseInfoAPI.area = model3.areaName;
    self.payRegisterBaseInfoAPI.areaCode = model3.areaCode;
    
    self.payRegisterBaseInfoAPI.doorPic = self.doorPic;
    self.payRegisterBaseInfoAPI.insidePic = self.insidePic;
    self.payRegisterBaseInfoAPI.cashierDeskPic = self.cashierDeskPic;
    
    self.payRegisterBaseInfoAPI.merchantName = self.merchantNameTv.text;
    self.payRegisterBaseInfoAPI.merchantShortName = self.merchantAsNameTv.text;
    self.payRegisterBaseInfoAPI.address = self.adressTv.text;
    self.payRegisterBaseInfoAPI.merchantId = [SAValueManger shareInStance].userInfo.merchantId;
    [self.payRegisterBaseInfoAPI sendRequestToServerWithActivityViewAndMask:POST];
}

-(void)clickCloseBtn:(UIButton *)sender {
    if (self.merchantCloseBtn == sender) {
        self.merchantNameTv.text = @"";
        self.merchantCloseBtn.hidden = YES;
    }else if (self.nameCloseBtn == sender) {
        self.merchantAsNameTv.text = @"";
        self.nameCloseBtn.hidden = YES;
    }else if (self.adressCloseBtn == sender) {
        self.adressTv.text = @"";
        self.adressCloseBtn.hidden = YES;
    }
}

-(void)clickedWechatBtn {
    SAConsultationWechatVC *pushVC = [SAConsultationWechatVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}

#pragma mark - initData
-(void)initData {
    if ([self.model.merchantDetailVO.baseInfoStatus isEqualToString:@"1"]) {
        self.merchantNameTv.text = self.model.merchantBaseInfoVO.merchantName;
        self.merchantAsNameTv.text = self.model.merchantBaseInfoVO.merchantShortName;
        self.cityTv.text = [NSString stringWithFormat:@"%@%@%@",self.model.merchantBaseInfoVO.province,self.model.merchantBaseInfoVO.city,self.model.merchantBaseInfoVO.area];
        
        [self.areaMarr removeAllObjects];
        SAPayRegisterAreaModel *model = [SAPayRegisterAreaModel new];
        model.areaCode = self.model.merchantBaseInfoVO.provinceCode;
        model.areaName = self.model.merchantBaseInfoVO.province;
        [self.areaMarr addObject:model];
        SAPayRegisterAreaModel *model2 = [SAPayRegisterAreaModel new];
        model2.areaCode = self.model.merchantBaseInfoVO.cityCode;
        model2.areaName = self.model.merchantBaseInfoVO.city;
        [self.areaMarr addObject:model2];
        SAPayRegisterAreaModel *model3 = [SAPayRegisterAreaModel new];
        model3.areaCode = self.model.merchantBaseInfoVO.areaCode;
        model3.areaName = self.model.merchantBaseInfoVO.area;
        [self.areaMarr addObject:model3];
        
        self.adressTv.text = self.model.merchantBaseInfoVO.address;
        [self.imageV sd_setImageWithURL:[NSURL URLWithString:self.model.merchantBaseInfoVO.doorPicUrl] placeholderImage:[UIImage imageNamed:@"pay_shop_1"]];
        [self.imageV2 sd_setImageWithURL:[NSURL URLWithString:self.model.merchantBaseInfoVO.insidePicUrl] placeholderImage:[UIImage imageNamed:@"pay_shop_2"]];
        [self.imageV3 sd_setImageWithURL:[NSURL URLWithString:self.model.merchantBaseInfoVO.cashierDeskPicUrl] placeholderImage:[UIImage imageNamed:@"pay_shop_3"]];
        self.doorPic = self.model.merchantBaseInfoVO.doorPic;
        self.insidePic = self.model.merchantBaseInfoVO.insidePic;
        self.cashierDeskPic = self.model.merchantBaseInfoVO.cashierDeskPic;
        [self showNext];
        [self.selectAddressView refreshData:self.areaMarr];
    }else if ([self.model.merchantDetailVO.legalInfoStatus isEqualToString:@"1"]) {
        self.merchantNameTv.text = self.model.merchantLegalPersonVO.legalName;
    }
    
    WeakSelf
    self.selectAddressView.successBlock = ^(NSMutableArray<SAPayRegisterAreaModel *> *marr, NSMutableArray *regionMarr) {
        weakSelf.areaMarr = marr;
        weakSelf.regionMarr = regionMarr;
        weakSelf.cityTv.text = [NSString stringWithFormat:@"%@%@%@",marr[0].areaName,marr[1].areaName,marr[2].areaName];
    };
    
    if ([self.model.merchantDetailVO.auditStatus isEqualToString:@"0"] || [self.model.merchantDetailVO.auditStatus isEqualToString:@"1"]) {
        self.merchantNameTv.userInteractionEnabled = NO;
        self.merchantAsNameTv.userInteractionEnabled = NO;
        self.rightBtn.userInteractionEnabled = NO;
        self.adressTv.userInteractionEnabled = NO;
        self.imageV.userInteractionEnabled = NO;
        self.imageV2.userInteractionEnabled = NO;
        self.imageV3.userInteractionEnabled = NO;
    }
    
    self.merchantCloseBtn.hidden = YES;
    self.nameCloseBtn.hidden = YES;
    self.adressCloseBtn.hidden = YES;
}

#pragma mark - initUI
-(void)initUI {
    
    [self setNavigationItemBackBBIAndTitle:@"经营信息"];
    self.view.backgroundColor = kLineColor;
    
    UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight + 6, kBaseScreenWidth, 163)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    
    NSArray *arr = @[@"商户名称", @"商户简称", @"所在城市", @"详细地址"];
    for (int i = 0; i < arr.count; i++) {
        UILabel *lab1 = [[UILabel alloc] initWithFrame:SARectMake(26, i * 41, 100, 40)];
        lab1.text = arr[i];
        lab1.font = kFont(13);
        lab1.textColor = kBlackColor;
        lab1.textAlignment = NSTextAlignmentLeft;
        [whiteView addSubview:lab1];
        
        YYTextView *textTv = [[YYTextView alloc] initWithFrame:SARectMake(127, 41 * i, 205, 40)];
        textTv.font = kFont(13);
        textTv.delegate = self;
        [whiteView addSubview:textTv];
        
        if (i == 0) {
            self.merchantNameTv = textTv;
            textTv.placeholderText = @"商品名称为身份证上的姓名";
        }else if (i == 1) {
            self.merchantAsNameTv = textTv;
            textTv.placeholderText = @"商户简称说明";
        }else if (i == 2) {
            self.cityTv = textTv;
            textTv.placeholderText = @"选择所在城市";
            self.cityTv.userInteractionEnabled = NO;
        }else if (i == 3) {
            self.adressTv = textTv;
            textTv.placeholderText = @"请输入5字以上的详细地址";
        }
        
        //让一开始的光标居中,必须text非空才可以生效
        textTv.text = @"     ";
        [self contentSizeToFit:textTv];
        textTv.text = @"";
        
        if (i != 3) {
            UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 40 + 41 * i, kBaseScreenWidth, 1)];
            line.backgroundColor = kLineColor;
            line.userInteractionEnabled = NO;
            [whiteView addSubview:line];
        }
        if (i == 2) {
            UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [rightBtn setImage:[UIImage imageNamed:@"pay_arrow_right"] forState:UIControlStateNormal];
            rightBtn.frame = SARectMake(0, 41 * i, kBaseScreenWidth, 40);
            rightBtn.titleLabel.font = kFont(15);
            [rightBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
            rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, kScaleNum(13));
            [rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
            self.rightBtn = rightBtn;
            [whiteView addSubview:rightBtn];
        }else {
            UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [closeBtn setImage:[UIImage imageNamed:@"btn_cancle"] forState:UIControlStateNormal];
            closeBtn.frame = SARectMake(kBaseScreenWidth - 40, 41 * i, 40, 40);
            closeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [closeBtn addTarget:self action:@selector(clickCloseBtn:) forControlEvents:UIControlEventTouchUpInside];
            [whiteView addSubview:closeBtn];
            closeBtn.hidden = YES;
            if (i == 0) {
                self.merchantCloseBtn = closeBtn;
            }else if (i == 1) {
                self.nameCloseBtn = closeBtn;
            }else if (i == 3) {
                self.adressCloseBtn = closeBtn;
            }
        }
    }
    
    UILabel *tipLab = [[UILabel alloc] initWithFrame:SARectMake(26, StatusHeight + 184, kBaseScreenWidth - 26, 15)];
    tipLab.text = @"上传摊位照片 (照片清晰，不可截图，否者会审核失败)";
    tipLab.font = kFont(14);
    tipLab.textColor = kBlackColor;
    tipLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:tipLab];
    [tipLab setAttributedStringWithSubString:@"(照片清晰，不可截图，否者会审核失败)" color:kPinkColor font:kFont(12)];
    
    NSArray *arr2 = @[@"1、", @"2、", @"3、"];
    NSArray *arr3 = @[@"门头照", @"经营场所内照片", @"收银台照片"];
    NSArray *arr4 = @[@"需展示完整的牌匾、店名", @"需清晰反映店内经营商品或环境\n请拍摄整体照片，不要拍摄局部", @"需展示店内收款工具"];
    //    NSArray *arr = @[@"商户名称", @"商户简称", @"所在城市", @"详细地址"];
    for (int i = 0; i < 3; i++) {
        NSString *imageName = [NSString stringWithFormat:@"pay_shop_%d",i + 1];
        UIImageView *mainImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        mainImageV.frame = SARectMake(276, StatusHeight + 216 + 68 * i, 84, 53);
        mainImageV.userInteractionEnabled = YES;
        mainImageV.contentMode = UIViewContentModeScaleAspectFit;
        mainImageV.backgroundColor = kColor(210, 210, 210, 1);
        [self.view addSubview:mainImageV];
        
        if (i == 0) {
            self.imageV = mainImageV;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doorImage)];
            [mainImageV addGestureRecognizer:tap];
        }else if (i == 1) {
            self.imageV2 = mainImageV;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insideImage)];
            [mainImageV addGestureRecognizer:tap];
        }else if (i == 2) {
            self.imageV3 = mainImageV;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cashierDeskImage)];
            [mainImageV addGestureRecognizer:tap];
        }
        
        //        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(idcardOCROnlineFront)];
        //        [positiveImageV addGestureRecognizer:tap];
        //        self.positiveImageV = positiveImageV;
        
        UIImageView *cameraImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pay_camera"]];
        cameraImageV.frame = SARectMake(298, StatusHeight + 222 + 68 * i, 40, 40);
        cameraImageV.userInteractionEnabled = NO;
        cameraImageV.contentMode = UIViewContentModeScaleAspectFill;
        [self.view addSubview:cameraImageV];
        if ([self.model.merchantDetailVO.auditStatus isEqualToString:@"0"] || [self.model.merchantDetailVO.auditStatus isEqualToString:@"1"]) {
            cameraImageV.hidden = YES;
        }
        
        UILabel *lab1 = [[UILabel alloc] initWithFrame:SARectMake(26, StatusHeight + 216 + 68 * i, 30, 15)];
        lab1.text = arr2[i];
        lab1.font = kFont(14);
        lab1.textColor = kBlackColor;
        lab1.textAlignment = NSTextAlignmentLeft;
        [self.view addSubview:lab1];
        
        UILabel *lab2 = [[UILabel alloc] initWithFrame:SARectMake(46, StatusHeight + 216 + 68 * i, 210, 15)];
        lab2.text = arr3[i];
        lab2.font = kFont(14);
        lab2.textColor = kBlackColor;
        lab2.textAlignment = NSTextAlignmentLeft;
        [self.view addSubview:lab2];
        
        UILabel *lab3 = [[UILabel alloc] init];
        lab3.text = arr4[i];
        lab3.font = kFont(14);
        lab3.textColor = kLightgGrayColor;
        lab3.numberOfLines = 0;
        lab3.textAlignment = NSTextAlignmentLeft;
        [self.view addSubview:lab3];
        
        [lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kScaleNum(StatusHeight + 236 + 68 * i));
            make.left.mas_equalTo(kScaleNum(46));
        }];
    }
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"button_gray_bg"] forState:UIControlStateNormal];
    nextBtn.frame = CGRectMake(kScaleNum(70), kScaleNum(StatusHeight + 440), kScaleNum(235), kScaleNum(35));
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    nextBtn.titleLabel.font = kFont(16);
    nextBtn.userInteractionEnabled = NO;
    [nextBtn addTarget:self action:@selector(clickedNextBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    self.nextBtn = nextBtn;
    
    UIButton *wechatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    wechatBtn.frame = CGRectMake((kScreenWidth - kScaleNum(150))/2, kScaleNum(StatusHeight + 475), kScaleNum(150), kScaleNum(35));
    [wechatBtn setTitle:@"添加客服咨询>" forState:UIControlStateNormal];
    [wechatBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
    wechatBtn.titleLabel.font = kFont(13);
    [wechatBtn addTarget:self action:@selector(clickedWechatBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wechatBtn];
    
    [self.view addSubview:self.selectAddressView];
}

- (void)contentSizeToFit:(YYTextView *)textView
{
    //先判断一下有没有文字（没文字就没必要设置居中了）
    if([textView.text length]>0)
    {
        //textView的contentSize属性
        CGSize contentSize = textView.contentSize;
        //textView的内边距属性
        UIEdgeInsets offset;
        CGSize newSize = contentSize;
        
        //如果文字内容高度没有超过textView的高度
        if(contentSize.height <= textView.frame.size.height)
        {
            //textView的高度减去文字高度除以2就是Y方向的偏移量，也就是textView的上内边距
            CGFloat offsetY = (textView.frame.size.height - contentSize.height)/2;
            offset = UIEdgeInsetsMake(offsetY, 0, 0, 0);
        }
        else          //如果文字高度超出textView的高度
        {
            contentSize = textView.contentSize;
            offset = UIEdgeInsetsZero;
        }
        
        //根据前面计算设置textView的ContentSize和Y方向偏移量
        [textView setContentSize:newSize];
        [textView setContentInset:offset];
        
    }
}

-(SASelectAddressView *)selectAddressView {
    if (!_selectAddressView) {
        _selectAddressView = [[SASelectAddressView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _selectAddressView.hidden = YES;
    }
    return _selectAddressView;
}

-(NSMutableArray<SAPayRegisterAreaModel *> *)areaMarr {
    if (!_areaMarr) {
        _areaMarr = [NSMutableArray array];
    }
    return _areaMarr;
}

-(SAPayCommonUploadAPI *)payCommonUploadAPI {
    if (!_payCommonUploadAPI) {
        _payCommonUploadAPI = [SAPayCommonUploadAPI apiWithDelegate:self];
    }
    return _payCommonUploadAPI;
}

-(SAPayCommonUploadAPI *)payCommonUploadAPI2 {
    if (!_payCommonUploadAPI2) {
        _payCommonUploadAPI2 = [SAPayCommonUploadAPI apiWithDelegate:self];
    }
    return _payCommonUploadAPI2;
}

-(SAPayCommonUploadAPI *)payCommonUploadAPI3 {
    if (!_payCommonUploadAPI3) {
        _payCommonUploadAPI3 = [SAPayCommonUploadAPI apiWithDelegate:self];
    }
    return _payCommonUploadAPI3;
}

-(SAPayRegisterBaseInfoAPI *)payRegisterBaseInfoAPI {
    if (!_payRegisterBaseInfoAPI) {
        _payRegisterBaseInfoAPI = [SAPayRegisterBaseInfoAPI apiWithDelegate:self];
    }
    return _payRegisterBaseInfoAPI;
}

-(SAPayRegisterDetailAPI *)payRegisterDetailAPI {
    if (!_payRegisterDetailAPI) {
        _payRegisterDetailAPI = [SAPayRegisterDetailAPI apiWithDelegate:self];
    }
    return _payRegisterDetailAPI;
}

@end
